$(document).ready(function(){

	initMenu();
	
});

$(function (){
	//alert("alert2");
});

$(document).ajaxComplete(function(){
	openCurrentMenu();
});


function openCurrentMenu() {
	//var currentUrl = '';
	//currentUrl = window.location.pathname
	//alert(currentUrl);
	$('.sidebar-menu').tree();
	  $('.sidebar-menu').filter(".menu-open").removeClass("menu-open");
	  $('.sidebar-menu').filter(".active").removeClass("active");
	  var a;
	  $('.sidebar-menu a').each(function(i){
		    if($(this).attr('href')==window.location.pathname) {
				a=$(this);
			}
	  });
	  a.parent().addClass("active");
	  a.parent().parent().parent().addClass("active");
	  a.parent().parent().parent().addClass("menu-open");
}



function initMenu() {
	var vmenuhtml = "";
	$.getJSON("/json/menus.json",function(data){
		//alert(JSON.stringify(data));
		$.each(data,function(i,item){
				var tprocess = process(item);
				vmenuhtml = vmenuhtml + tprocess;
		});
		//alert(vmenuhtml)
		//console.log(vmenuhtml);
		$('.sidebar .sidebar-menu').append(vmenuhtml);
	})
	
}


function process(item) {
	
	if (item.type == 0) {
		//菜单目录处理
		let tsubicon0 = '<i></i>';
		if (item.icon != "") {
			tsubicon0 = '<i class="' + item.icon + '"></i>';
		}
		let tsubname0 = '<span></span>';
		if (item.name != "") {
			tsubname0 = '<span>' + item.name + '</span>';
		}
		let t00 = '<li class="treeview"><a id="' + 'menuId'+item.menuId + '"href="#">' + tsubicon0 + tsubname0 + '<span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span></a>';
		let t01 = '<ul class="treeview-menu">';
		let t02 = '';
		if (item.list != '') {
			// 目录含有子菜单的处理
			$.each(item.list, function(i,subitem){
				let t0t = process(subitem);
				t02 = t02 + t0t;
			});
		}
		let t03 = '</ul></li>';
		let t0all = t00 + t01 + t02 + t03;
		return t0all;
	} else if (item.type == 1) {
		// 菜单连接处理
		let tsubicon1 = '<i></i>';
		if (item.icon != "") {
			tsubicon1 = '<i class="' + item.icon + '"></i>';
		}
		let tsubname1 = '<span></span>';
		if (item.name != "") {
			tsubname1 = '<span>' + item.name + '</span>';
		}
		let t11 = '<li><a id="' + 'menuId'+item.menuId + '" href="' + item.url + '">' + tsubicon1 + tsubname1 + '</a></li>';
		return t11;
	}
	
}


function logout() {
	top.location.href="/logout.html";
}

